﻿<%@ WebHandler Language="C#" Class="SQCDPDetails" %>

using System;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.SessionState;
using ReportBusiness;

public class SQCDPDetails : IHttpHandler
{
    public bool IsReusable { get { return false; } }
    private uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public void ProcessRequest(HttpContext context)
    {
        //指标点检
        if (context.Request.Form["type"] != null && context.Request.Form["type"] == "GetZhiBiaoMaint")
        {
            Dictionary<string, string> para = new Dictionary<string, string>();
            if (context.Request.Form["page"] != null)
            {
                para["page"] = context.Request.Form["page"].ToString();
            }
            if (context.Request.Form["rows"] != null)
            {
                para["rows"] = context.Request.Form["rows"].ToString();
            }
            //指标项名称
            if (context.Request.Form["itemname"] != null)
            {
                para["itemname"] = context.Request.Form["itemname"].ToString();
            }
            //工区
            if (context.Request.Form["workcenter"] != null)
            {
                para["workcenter"] = context.Request.Form["workcenter"].ToString();
            }
            int pageCount = 0;
            int rowCount = 0;

            string sql = @"
SELECT substr(fm.yearmonth,5,2) || '月' planmonth,wmd.completedate,REPLACE(w.workcentername,'装试','') workcentername,
       wm.itemname,wm.planvalue,wm.safevalue,wmd.actualvalue,decode(wmd.status,0,'未提交',1,'已完成',2,'存在问题') status
FROM sqcdp_workcenter_maint_details wmd
LEFT JOIN sqcdp_workcenter_maint wm ON wm.sqcdp_workcenter_maintid = wmd.sqcdp_workcenter_maintid
LEFT JOIN sqcdp_factory_maint fm ON fm.sqcdp_factory_maintid = wm.sqcdp_factory_maintid
LEFT JOIN factory f ON f.factoryid = fm.factoryid
LEFT JOIN Workcenter w ON w.workcenterid = wm.workcenterid
WHERE w.workcentername IS NOT NULL AND f.factoryname='发动机总体事业部' 
      AND fm.yearmonth = (to_char(SYSDATE,'yyyy') || to_char(SYSDATE,'mm'))
      AND TRUNC(wmd.completedate) BETWEEN TRUNC(SYSDATE,'mm') AND last_day(TRUNC(SYSDATE))
";

            sql += string.Format(" AND wm.itemname='{0}' ", para["itemname"].ToString());
            sql += string.Format(" AND w.workcentername LIKE '%{0}%' ", para["workcenter"].ToString());

            sql += " ORDER BY wmd.completedate";
            uMESPagingDataDTO retR = dal.GetPagingDataIns(sql, Convert.ToInt32(para["page"]), Convert.ToInt32(para["rows"]));
            pageCount = Convert.ToInt32(retR.PageCount);
            rowCount = Convert.ToInt32(retR.RowCount);

            var json = JsonConvert.SerializeObject(retR.DBTable);
            context.Response.ContentType = "text/json";
            json = "{\"data\":" + json.ToString()
                    + ",\"count\":" + rowCount
                    + ",\"code\":" + "0"
                    + ",\"msg\":" + "0"
                    + "}";
            context.Response.Write(json.ToString());
            context.Response.End();
        }

        //指标点检问题
        if (context.Request.Form["type"] != null && context.Request.Form["type"] == "GetProblem")
        {
            Dictionary<string, string> para = new Dictionary<string, string>();
            if (context.Request.Form["page"] != null)
            {
                para["page"] = context.Request.Form["page"].ToString();
            }
            if (context.Request.Form["rows"] != null)
            {
                para["rows"] = context.Request.Form["rows"].ToString();
            }
            //指标项名称
            if (context.Request.Form["itemname"] != null)
            {
                para["itemname"] = context.Request.Form["itemname"].ToString();
            }
            //工区
            if (context.Request.Form["workcenter"] != null)
            {
                para["workcenter"] = context.Request.Form["workcenter"].ToString();
            }
            int pageCount = 0;
            int rowCount = 0;

            string sql = @"
SELECT t.*
FROM 
(
SELECT SUBSTR(fm.yearmonth,5,2) || '月' planmonth,wm.itemname,up.description,up.ufccreatedate,
       w2.workcentername resworkcentername,up.ufcresult,
       CASE WHEN up.ufcstatus=0 THEN '已完成' 
            WHEN up.ufcstatus=1 THEN '未处理' 
            WHEN up.ufcstatus=2 THEN '处理中' 
            WHEN up.ufcstatus=3 THEN '处理完' 
       END AS ufcstatus,
       up.ufcchulidate,up.ufcwanchengdate,'指标维护' datafrom
FROM sqcdp_workcenter_maint_details wmd
LEFT JOIN sqcdp_workcenter_maint wm ON wm.sqcdp_workcenter_maintid = wmd.sqcdp_workcenter_maintid
LEFT JOIN sqcdp_factory_maint fm ON fm.sqcdp_factory_maintid = wm.sqcdp_factory_maintid
LEFT JOIN factory f ON f.factoryid = fm.factoryid
LEFT JOIN Workcenter w ON w.workcenterid = wm.workcenterid
LEFT JOIN ufcproblem up ON up.problemfeedbackdetailid = wmd.id
LEFT JOIN Workcenter w2 ON w2.workcenterid = up.workcenterid
WHERE w.workcentername IS NOT NULL AND f.factoryname='发动机总体事业部' 
      AND fm.yearmonth = (to_char(SYSDATE,'yyyy') || to_char(SYSDATE,'mm'))
      AND TRUNC(wmd.completedate) BETWEEN TRUNC(SYSDATE,'mm') AND last_day(TRUNC(SYSDATE))
      AND wmd.status = 2

UNION ALL

SELECT to_char(mfd.completedate,'mm') || '月' planmonth,mf.operationcontent itemname,up.description,up.ufccreatedate,
       w2.workcentername resworkcentername,up.ufcresult,
       CASE WHEN up.ufcstatus=0 THEN '已完成' 
            WHEN up.ufcstatus=1 THEN '未处理' 
            WHEN up.ufcstatus=2 THEN '处理中' 
            WHEN up.ufcstatus=3 THEN '处理完' 
       END AS ufcstatus,
       up.ufcchulidate,up.ufcwanchengdate,'管理者标准作业' datafrom
FROM manageroperationflowcomplete mfd
LEFT JOIN manageroperationflow mf ON mf.manageroperationflowid = mfd.manageroperationflowid
LEFT JOIN sessionvalues sv ON sv.employeeid = mfd.createemployeeid--mf.operateemployee
LEFT JOIN factory f ON f.factoryid = sv.factoryid
LEFT JOIN workcenter w ON w.workcenterid = sv.workcenterid
LEFT JOIN ufcproblem up ON up.problemfeedbackdetailid = mfd.manageroperationflowcompleteid
LEFT JOIN Workcenter w2 ON w2.workcenterid = up.workcenterid
WHERE w.workcentername IS NOT NULL AND f.factoryname='发动机总体事业部' 
      AND TRUNC(mfd.completedate) BETWEEN TRUNC(SYSDATE,'mm') AND last_day(TRUNC(SYSDATE))
      AND mfd.status = 2
) t
WHERE 1=1
";

            sql += string.Format(" AND t.itemname='{0}' ", para["itemname"].ToString());
            sql += string.Format(" AND t.resworkcentername LIKE '%{0}%' ", para["workcenter"].ToString());

            uMESPagingDataDTO retR = dal.GetPagingDataIns(sql, Convert.ToInt32(para["page"]), Convert.ToInt32(para["rows"]));
            pageCount = Convert.ToInt32(retR.PageCount);
            rowCount = Convert.ToInt32(retR.RowCount);

            var json = JsonConvert.SerializeObject(retR.DBTable);
            context.Response.ContentType = "text/json";
            json = "{\"data\":" + json.ToString()
                    + ",\"count\":" + rowCount
                    + ",\"code\":" + "0"
                    + ",\"msg\":" + "0"
                    + "}";
            context.Response.Write(json.ToString());
            context.Response.End();
        }

        //管理者标准作业
        if (context.Request.Form["type"] != null && context.Request.Form["type"] == "GetGuanBiao")
        {
            Dictionary<string, string> para = new Dictionary<string, string>();
            if (context.Request.Form["page"] != null)
            {
                para["page"] = context.Request.Form["page"].ToString();
            }
            if (context.Request.Form["rows"] != null)
            {
                para["rows"] = context.Request.Form["rows"].ToString();
            }
            //指标项名称
            if (context.Request.Form["itemname"] != null)
            {
                para["itemname"] = context.Request.Form["itemname"].ToString();
            }
            //工区
            if (context.Request.Form["workcenter"] != null)
            {
                para["workcenter"] = context.Request.Form["workcenter"].ToString();
            }
            int pageCount = 0;
            int rowCount = 0;

            string sql = @"
SELECT to_char(mfd.completedate,'mm') || '月' planmonth,mf.operationcontent itemname,w.workcentername,
       mfd.completedate,DECODE(mfd.status,0,'未完成','已完成') status
FROM manageroperationflowcomplete mfd
LEFT JOIN manageroperationflow mf ON mf.manageroperationflowid = mfd.manageroperationflowid
LEFT JOIN sessionvalues sv ON sv.employeeid = mfd.createemployeeid--mf.operateemployee
LEFT JOIN factory f ON f.factoryid = sv.factoryid
LEFT JOIN workcenter w ON w.workcenterid = sv.workcenterid
WHERE w.workcentername IS NOT NULL AND f.factoryname='发动机总体事业部' 
      AND TRUNC(mfd.completedate) BETWEEN TRUNC(SYSDATE,'mm') AND last_day(TRUNC(SYSDATE))
";

            sql += string.Format(" AND mf.operationcontent='{0}' ", para["itemname"].ToString());
            sql += string.Format(" AND w.workcentername LIKE '%{0}%' ", para["workcenter"].ToString());

            uMESPagingDataDTO retR = dal.GetPagingDataIns(sql, Convert.ToInt32(para["page"]), Convert.ToInt32(para["rows"]));
            pageCount = Convert.ToInt32(retR.PageCount);
            rowCount = Convert.ToInt32(retR.RowCount);

            var json = JsonConvert.SerializeObject(retR.DBTable);
            context.Response.ContentType = "text/json";
            json = "{\"data\":" + json.ToString()
                    + ",\"count\":" + rowCount
                    + ",\"code\":" + "0"
                    + ",\"msg\":" + "0"
                    + "}";
            context.Response.Write(json.ToString());
            context.Response.End();
        }
    }
}